﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace HHCommon
{
    public class EncAndDec
    {
        #region //对称加密算法
        /// <summary>
        /// DESCryptoServiceProvider加密类
        /// </summary>
        /// <param name="strSource">明文</param>
        /// <returns>返回密文</returns>
        public string DESEncrypting(string strSource)
        {
            //把字符串放入byte数组中
            byte[] bytIn = System.Text.Encoding.Default.GetBytes(strSource);
            //建立加密对象的密钥和偏移量
            byte[] iv = {102,16,93,156,78,4,208,32 };//偏移量
            byte[] key = {55,103,246,79,36,99,167,3 };//密钥
            //实例DES加密类
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            des.IV = iv;
            des.Key = key;
            //创建一个加密对象(加密转换运算)
            ICryptoTransform encrypto = des.CreateEncryptor();
            //实例MemoryStream内存流加密文件
            System.IO.MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
            cs.Write(bytIn, 0, bytIn.Length);
            cs.FlushFinalBlock();
            return System.Convert.ToBase64String(ms.ToArray());
        }

        /// <summary>
        /// RijndaelManaged高级加密标准加密类
        /// </summary>
        /// <param name="strSource">明文</param>
        /// <returns>密文</returns>
        public string RijndaelEncrypting(string strSource)
        {
            //把字符串放入byte数组中
            byte[] bytIn = System.Text.Encoding.Default.GetBytes(strSource);
            //建立加密对象的密钥和偏移量
            byte[] iv = {24,55,102,24,98,26,67,29,84,19,37,118,104,85,121,27,93,86,24,55,102,24,98,26,67,29,9,2,49,73,92 };//偏移量
            byte[] key = {22,56,82,77,84,31,74,24,55,102,24,98,26,67,29,99};//密钥
            //实例RijndaelManaged加密类
            RijndaelManaged myRijndael = new RijndaelManaged();
            myRijndael.IV = iv;
            myRijndael.Key = key;
            //创建一个加密对象
            ICryptoTransform encrypto = myRijndael.CreateEncryptor();
            //实例MemoryStream内存流加密文件
            System.IO.MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
            cs.Write(bytIn, 0, bytIn.Length);
            cs.FlushFinalBlock();
            byte[] bytOut = ms.ToArray();
            return System.Convert.ToBase64String(bytOut);
        }
        /// <summary>
        /// TripleDESCryptoServiceProvider加密类
        /// </summary>
        /// <param name="strSource">明文</param>
        /// <returns>密码</returns>
        public string TripleDESEncrypting(string strSource)
        {
            //把字符串放入byte数组中
            byte[] bytIn = System.Text.Encoding.Default.GetBytes(strSource);
            //建立加密对象的密钥和偏移量，采用24字节或192位的密钥和初始向量
            byte[] iv = {42,16,93,156,78,4,218,32,15,167,44,80,26,20,155,2,94,11,204,119,35,184,197};//偏移量
            byte[] key = {55,103,246,79,36,99,167,3 };//密钥
            //实例TripleDESCryptoServiceProvider加密类
            TripleDESCryptoServiceProvider TripleDES = new TripleDESCryptoServiceProvider();
            TripleDES.IV = iv;
            TripleDES.Key = key;
            //创建一个加密对象
            ICryptoTransform encrypto = TripleDES.CreateEncryptor();
            //实例MemoryStream内存流加密文件
            System.IO.MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
            cs.Write(bytIn, 0, bytIn.Length);
            cs.FlushFinalBlock();
            byte[] bytOut = ms.ToArray();
            return System.Convert.ToBase64String(bytOut);
        }
        /// <summary>
        /// RC2CryptoServiceProvider加密类
        /// </summary>
        /// <param name="strSource">明文</param>
        /// <returns>密文</returns>
        public string RC2Encrypting(string strSource)
        {
            //把字符串放入byte数组中
            byte[] bytIn = System.Text.Encoding.Default.GetBytes(strSource);
            //建立加密对象的密钥和偏移量,采用24字节或192位的密钥和初始向量
            byte[] iv = {53,68,57,79,159,168,157,179,201,40,0,123,210,189,167,1};//偏移量
            byte[] key = {5,13,46,97,69,109,177,39 };//密钥
            //实例RC2CryptoServiceProvider加密类
            RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
            rc2.IV = iv;
            rc2.Key = key;
            //创建一个加密对象
            ICryptoTransform encrypto = rc2.CreateEncryptor();
            //实例MemoryStream内存流加密文件
            System.IO.MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
            cs.Write(bytIn, 0, bytIn.Length);
            cs.FlushFinalBlock();
            byte[] bytOut = ms.ToArray();
            return System.Convert.ToBase64String(bytOut);
        }
        #endregion

        #region //对称解密算法
        /// <summary>
        /// DESCryptoServiceProvider解密类
        /// </summary>
        /// <param name="strSource">密文</param>
        /// <returns>明文</returns>
        public string DESDecrypting(string strSource)
        {
            //将加密字符串转化成byte数组
            byte[] bytIn = System.Convert.FromBase64String(strSource);
            //给出解密的密钥和偏移量。注意：密钥和偏移量必须与加密时密钥和偏移量相同
            byte[] iv = { 102, 16, 93, 156, 78, 4, 208, 32 };//偏移量
            byte[] key = { 55, 103, 246, 79, 36, 99, 167, 3 };//密钥
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            des.IV = iv;
            des.Key = key;
            System.IO.MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
            ICryptoTransform encrypto = des.CreateDecryptor();
            CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
            StreamReader strd = new StreamReader(cs, Encoding.Default);
            return strd.ReadToEnd();
        }
        /// <summary>
        /// RijndaelManaged解密类
        /// </summary>
        /// <param name="strSource">密文</param>
        /// <returns>明文</returns>
        public string RijndaelDecrypting(string strSource)
        {
            //将加密字符串转化成byte数组
            byte[] bytIn = System.Convert.FromBase64String(strSource);
            //给出解密的密钥和偏移量。注意：密钥和偏移量必须与加密时密钥和偏移量相同
            byte[] iv = { 24, 55, 102, 24, 98, 26, 67, 29, 84, 19, 37, 118, 104, 85, 121, 27, 93, 86, 24, 55, 102, 24, 98, 26, 67, 29, 9, 2, 49, 73, 92 };//偏移量
            byte[] key = { 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 24, 98, 26, 67, 29, 99 };//密钥
            RijndaelManaged Rijndael = new RijndaelManaged();
            Rijndael.IV = iv;
            Rijndael.Key = key;
            System.IO.MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
            ICryptoTransform encrypto = Rijndael.CreateDecryptor();
            CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
            StreamReader strd = new StreamReader(cs, Encoding.Default);
            return strd.ReadToEnd();
        }
        /// <summary>
        /// TripleDESCryptoServiceProvider解密类
        /// </summary>
        /// <param name="strSource">密文</param>
        /// <returns>明文</returns>
        public string TripleDESDESDecrypting(string strSource)
        {
            //将加密字符串转化成byte数组
            byte[] bytIn = System.Convert.FromBase64String(strSource);
            //给出解密的密钥和偏移量。注意：密钥和偏移量必须与加密时密钥和偏移量相同
            byte[] iv = { 42, 16, 93, 156, 78, 4, 218, 32, 15, 167, 44, 80, 26, 20, 155, 2, 94, 11, 204, 119, 35, 184, 197 };//偏移量
            byte[] key = { 55, 103, 246, 79, 36, 99, 167, 3 };//密钥
            TripleDESCryptoServiceProvider TripleDES = new TripleDESCryptoServiceProvider();
            TripleDES.IV = iv;
            TripleDES.Key = key;
            System.IO.MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
            ICryptoTransform encrypto = TripleDES.CreateDecryptor();
            CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
            StreamReader strd = new StreamReader(cs, Encoding.Default);
            return strd.ReadToEnd();
        }
        /// <summary>
        /// RC2CryptoServiceProvider解密类
        /// </summary>
        /// <param name="strSource">密文</param>
        /// <returns>明文</returns>
        public string RC2Decrypting(string strSource)
        {
            //将加密字符串转化成byte数组
            byte[] bytIn = System.Convert.FromBase64String(strSource);
            //给出解密的密钥和偏移量。注意：密钥和偏移量必须与加密时密钥和偏移量相同
            byte[] iv = { 53, 68, 57, 79, 159, 168, 157, 179, 201, 40, 0, 123, 210, 189, 167, 1 };//偏移量
            byte[] key = { 5, 13, 46, 97, 69, 109, 177, 39 };//密钥
            RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
            rc2.IV = iv;
            rc2.Key = key;
            System.IO.MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
            ICryptoTransform encrypto = rc2.CreateDecryptor();
            CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read);
            StreamReader strd = new StreamReader(cs, Encoding.Default);
            return strd.ReadToEnd();
        }

        #endregion

        #region //非对称加密算法
        #endregion

        #region //非对称加密算法
        #endregion
    }
}
